$('.dualselect').find('.ds_arrow .arrow').live('click', function() {
	var sel1 = $(this).parent().parent().find('select:first-child');
	var sel2 = $(this).parent().parent().find('select:last-child');
	var t = ($(this).hasClass('ds_prev'))? 0 : 1;	// 0 if arrow prev otherwise arrow next
	if(t) {
		sel1.find('option').each(function(){
			if(jQuery(this).is(':selected')) {
				jQuery(this).attr('selected',false);
				var op = sel2.find('option:first-child');
				sel2.append(jQuery(this));
			}
		});	
	} else {
		sel2.find('option').each(function(){
			if(jQuery(this).is(':selected')) {
				jQuery(this).attr('selected',false);
				sel1.append(jQuery(this));
			}
		});		
	}			
})

var getDeptCategories = function(departmentId) {
	var roleCategories = '';
	var isFirst = true;
	var comma;
	$('select[name="categories_second"] option').each(function(k, v) {				
		comma = !isFirst?',':'';
		roleCategories += comma + $(v).val();				
		isFirst = false;
	})			
	$.post(
		'/admin/ajax/get-all-categories', 
		{
			department_id: departmentId,
			role_categories: roleCategories
		}, 
		function(response) {				
			categories = JSON.parse(response);
			fillDeptSelect(categories);
		}
	)			
}

var fillDeptSelect = function(data) {
	var options = '';
	for (var i in data) {
		var category = data[i];				
		options += '<option value="'+category.id+'">';
		options += category.category_name
		options += '</option>';				
	}
	$('select[name="categories_first"]').html(options);
}

$('#departments').live('change', function() {
	var departmentId = $('#departments option:selected').val();
	getDeptCategories(departmentId);	
})		

var getFormData = function() {
	var data = {};
	var categories = [];
	var departments = [];
	var resources = [];
	var types = [];

	$('select[name="departments_second"] option').each(function(k, v) {				
		departments[k] = $(v).val();				
	})	
	data.departments = departments;

	$('select[name="resources_second"] option').each(function(k, v) {				
		resources[k] = $(v).val();				
	})	
	data.resources = resources;

	$('select[name="types_second"] option').each(function(k, v) {				
		types[k] = $(v).val();				
	})	
	data.types = types;

	$('select[name="categories_second"] option').each(function(k, v) {				
		categories[k] = $(v).val();				
	})	
	data.categories = categories;

	data.role_name = $('input[name="role_name"]').val();
	return data;
}
